[bits 32]

global asm_hello_world
global asm_lidt
global asm_unhandled_interrupt
global asm_halt

ASM_UNHANDLED_INTERRUPT_INFO db 'Unhandled interrupt happened, halt...'
                             db 0
ASM_IDTR dw 0
         dd 0

; void asm_unhandled_interrupt()
asm_unhandled_interrupt:
    cli
    mov esi, ASM_UNHANDLED_INTERRUPT_INFO
    xor ebx, ebx
    mov ah, 0x03
.output_information:
    cmp byte[esi], 0
    je .end
    mov al, byte[esi]
    mov word[gs:bx], ax
    inc esi
    add ebx, 2
    call asm_hello_world
    jmp .output_information
.end:
    jmp $

; void asm_lidt(uint32 start, uint16 limit)
asm_lidt:
    push ebp
    mov ebp, esp
    push eax

    mov eax, [ebp + 4 * 3]
    mov [ASM_IDTR], ax
    mov eax, [ebp + 4 * 2]
    mov [ASM_IDTR + 2], eax
    lidt [ASM_IDTR]

    pop eax
    pop ebp
    ret

asm_hello_world:
    push eax
    xor eax, eax

    mov ah, 0x03 ;青色
    mov al, '2'
    mov [gs:2 * (80*1+1)], ax

    mov al, '2'
    mov [gs:2 * (80*1+2)], ax

    mov al, '3'
    mov [gs:2 * (80*1+3)], ax

    mov al, '3'
    mov [gs:2 * (80*1+4)], ax

    mov al, '6'
    mov [gs:2 * (80*1+5)], ax

    mov al, '2'
    mov [gs:2 * (80*1+6)], ax

    mov al, '5'
    mov [gs:2 * (80*1+7)], ax

    mov al, '8'
    mov [gs:2 * (80*1+8)], ax

    mov al, 'x'
    mov [gs:2 * (80*1+9)], ax

    mov al, 'y'
    mov [gs:2 * (80*1+10)], ax

    mov al, 'b'
    mov [gs:2 * (80*1+11)], ax

    pop eax
    ret

asm_halt:
    jmp $
